package com.nuanshui.heatedloan.controller.display.credit;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.nuanshui.framework.lang.SysStringUtils;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.controller.BaseController;
import com.nuanshui.heatedloan.credit.facade.CustomerAddressListInfoService;
import com.nuanshui.heatedloan.credit.form.CustomerAddressListInfoForm;
import com.nuanshui.heatedloan.credit.form.CustomerCallRecordListInfoForm;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by wangxw on 2017/7/6.
 */
@RequestMapping("credit")
@RestController
public class CustomerAddressListController extends BaseController {

    @Reference(url= GlobalParam.CREDIT_DUBBO_SERVER,timeout= GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.CREDIT_DUBBO_SERVER_VERSION)
    private CustomerAddressListInfoService customerAddressListInfoService;

    @RequestMapping(value = "saveBatch/addressList",method = RequestMethod.POST)
    public String batchSaveCustomerAddressList(String customerAddressListStr){
       String userId=getCurrentUser().getId();
       if(SysStringUtils.isEmpty(customerAddressListStr)){
           return error("请传入参数");
       }
        List<CustomerAddressListInfoForm> customerAddressListInfos=new ArrayList<CustomerAddressListInfoForm>();
       customerAddressListInfos= JSON.parseArray(customerAddressListStr,CustomerAddressListInfoForm.class);
        for(CustomerAddressListInfoForm customerAddressListInfoForm:customerAddressListInfos){
            customerAddressListInfoForm.setUserId(userId);
        }
        customerAddressListInfoService.sendAddressListMsg(customerAddressListInfos);
        return success();
    }

    @RequestMapping(value = "/saveBatch/callRecord",method = RequestMethod.POST)
    public String batchSaveCustomerCallRecordList(String customerCallRecordListStr){
        String userId=getCurrentUser().getId();
        if(SysStringUtils.isEmpty(customerCallRecordListStr)){
            return error("请传入参数");
        }
        List<CustomerCallRecordListInfoForm> customerCallRecordListInfoForms=new ArrayList<>();
        customerCallRecordListInfoForms=JSON.parseArray(customerCallRecordListStr,CustomerCallRecordListInfoForm.class);
        if(customerCallRecordListInfoForms.size()!=0){
            customerAddressListInfoService.sendCallRecordListMsg(customerCallRecordListInfoForms);
        }
        return success();
    }

    @RequestMapping(value = "/judgeSuccess",method = RequestMethod.POST)
    public String judgeIsInsertSuccess(int type){
        String userId=getCurrentUser().getId();
        Map<String,Object> isSuccess=customerAddressListInfoService.getbatchInsertAddressListResult(userId,type);
        return success(isSuccess);
    }

    public static void main(String[] args) {
        List<CustomerAddressListInfoForm> customerAddressListInfoForms=new ArrayList<CustomerAddressListInfoForm>();
        CustomerAddressListInfoForm c=new CustomerAddressListInfoForm();
        c.setName("1");
        c.setPhone("15867273146");customerAddressListInfoForms.add(c);
        customerAddressListInfoForms.add(c);
        String str=JSON.toJSONString(customerAddressListInfoForms);
        System.out.println(str);

    }

}
